<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="面向对象综合实践">
<meta property="og:type" content="article">
<meta property="og:title" content="酒店前台客房管理系统1">
<meta property="og:url" content="http://yoursite.com/2020/04/17/%E9%85%92%E5%BA%97%E5%89%8D%E5%8F%B0%E5%AE%A2%E6%88%BF%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F1/index.html">
<meta property="og:site_name" content="很惭愧，就做了一点微小的工作">
<meta property="og:description" content="面向对象综合实践">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-04-16T16:38:09.000Z">
<meta property="article:modified_time" content="2020-05-17T15:42:51.807Z">
<meta property="article:author" content="文予">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/04/17/%E9%85%92%E5%BA%97%E5%89%8D%E5%8F%B0%E5%AE%A2%E6%88%BF%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F1/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>酒店前台客房管理系统1 | 很惭愧，就做了一点微小的工作</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">很惭愧，就做了一点微小的工作</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">念念不忘，必有回响</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/04/17/%E9%85%92%E5%BA%97%E5%89%8D%E5%8F%B0%E5%AE%A2%E6%88%BF%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/wenyu.png">
      <meta itemprop="name" content="文予">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="很惭愧，就做了一点微小的工作">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          酒店前台客房管理系统1
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-04-17 00:38:09" itemprop="dateCreated datePublished" datetime="2020-04-17T00:38:09+08:00">2020-04-17</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-17 23:42:51" itemprop="dateModified" datetime="2020-05-17T23:42:51+08:00">2020-05-17</time>
              </span>

          
            <div class="post-description">面向对象综合实践</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>设计一个酒店前台客房管理系统，包括酒店客房状态的查询，用户的入住、退房等功能。系统命令如下：</p>
<ul>
<li><code>search all</code>：查询并输出酒店所有客房的状态</li>
<li><code>search [客房编号]</code>：查询该客房状态</li>
<li><code>in [客房编号] [用户名]</code>：用户入住，例如，<code>in 0306 Grace</code>表示姓名为Grace的客人入住0306房间</li>
<li><code>out [客房编号]</code>：该客房退房，例如`</li>
<li><code>quit</code> 退出程序<br>入住时需要检测输入的客房编号是否正确，检测该客房是否已有客人。按客房编号进行查询的命令和退房命令同样需要检测输入的客房编号是否正确，检测该客房书否已有客人。</li>
</ul>
<h2 id="类的设计"><a href="#类的设计" class="headerlink" title="类的设计"></a>类的设计</h2><p>在这个系统中，每个客房有自己的客房编号和该客房入住的客人，所以将客房对象抽象为客房类<code>Room</code>，将客房编号和入住客人的姓名作为数组成员。客房是酒店的静态信息，并且这个信息在查询、入住、退房等方法中被共享，所以在酒店类<code>Hotel</code>中包含描述所有客房的数据成员了，即使用一个<code>Room</code>类型的二维数组存储所有的客房信息。<br>这里<code>Hotel</code>和<code>Room</code>类之间的关系可以用”has a”来描述，是面向对象设计中的一种组合(Composition)关系，即一个类的成员可以是其他类的引用。面向对象设计不提倡用单独的类来完成大量不相关的操作。<code>Hotel</code>类中包含了<code>Room</code>类的引用，客房的信息管理交给<code>Room</code>类，<code>Hotel</code>专心酒店本身的业务操作：查询，入住退房等等。</p>
<h2 id="客房编号的处理"><a href="#客房编号的处理" class="headerlink" title="客房编号的处理"></a>客房编号的处理</h2><p>在<code>Room</code>类中，书记成员id表示客房编号，为<code>String</code>类型；客房用户信息用<code>String</code>存储，即存储用户姓名，无人入住时为<code>null</code>，即String类型的默认初始值。</p>
<h3 id="客房编号"><a href="#客房编号" class="headerlink" title="客房编号"></a>客房编号</h3><p>因为<code>Hotel</code>中用<code>Room</code>的二维数组存储客房，所以设二维数组的行标为客房所在楼层，列标代表客房在该楼层的编号。<br>初始化<code>Room</code>对象的客房编号时，由二维数组的行标、列标拼成客房编号,如<code>i=0</code>、<code>j=0</code>时对应1层1号客房，编号为“0101”。拼接字符串的操作在<code>Room</code>类的<code>setId()</code>方法中完成。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> hotel;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Room</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String id;</span><br><span class="line">    <span class="keyword">private</span> String customerName;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(<span class="keyword">int</span> floor, <span class="keyword">int</span> number)</span> </span>&#123;</span><br><span class="line">        id = (floor &lt;= <span class="number">9</span> ? <span class="string">"0"</span> + floor : <span class="string">""</span> + floor) + (number &lt;= <span class="number">9</span> ? <span class="string">"0"</span> + number : <span class="string">""</span> + number);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="客房编号参数的解析"><a href="#客房编号参数的解析" class="headerlink" title="客房编号参数的解析"></a>客房编号参数的解析</h3><p>命令行输入的客房编号参数<code>roomNo</code>为<code>String</code>类型，为了加快运算速度，不采用在委任为数组中查询<code>Room</code>对象客房编号的方式，而是将合法的客房编号参数分解，前两位转换为它所对应的数组的行，后两位转换成列。<br>截取字符串的子串可以使用<code>String</code>类的<code>substring（）</code>方法，注意<code>substring（from,to）</code>截取的是左闭右开区间。<br>整数数字字符串转换为对应的整数，使用<code>int</code>类型的包装类型<code>Integer</code>的<code>parseInt()</code>方法。提取过程如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> height = Integer.parseInt(roomNo.substring(<span class="number">0</span>, <span class="number">2</span>));</span><br><span class="line"><span class="keyword">int</span> width = Integer.parseInt(roomNo.substring(<span class="number">2</span>, <span class="number">4</span>));</span><br></pre></td></tr></table></figure>

<h3 id="客房编号的合法性判断"><a href="#客房编号的合法性判断" class="headerlink" title="客房编号的合法性判断"></a>客房编号的合法性判断</h3><p>客房的楼层喝客房数由<code>Hotel</code>的<code>static</code>常量<code>HEIGHT</code>和<code>WDITH</code>指定，客房楼层编号在0<del>HEIGHT-1范围内，各层客房编号在0</del>WIDTH-1范围内。在查询、入住和退房操作中，都需要判断给定客房参数的合法性，一次设计如下的<code>Hotel</code>内部使用的方法<code>testRoomNo()</code>负责检测客房编号的合法性，供其他方法调用。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">testRoomNo</span><span class="params">(String roomNo)</span> </span>&#123; <span class="comment">// 客房编号合法性判断</span></span><br><span class="line">        <span class="keyword">int</span> height = Integer.parseInt(roomNo.substring(<span class="number">0</span>, <span class="number">2</span>));</span><br><span class="line">        <span class="keyword">int</span> width = Integer.parseInt(roomNo.substring(<span class="number">2</span>, <span class="number">4</span>));</span><br><span class="line">        <span class="keyword">if</span> (height &lt; <span class="number">1</span> || height &gt; HEIGHT || width &lt; <span class="number">1</span> || width &gt; WIDTH) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/04/13/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day7/" rel="prev" title="Java日拱一卒-day7">
      <i class="fa fa-chevron-left"></i> Java日拱一卒-day7
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/04/20/%E9%85%92%E5%BA%97%E5%89%8D%E5%8F%B0%E5%AE%A2%E6%88%BF%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F2/" rel="next" title="酒店前台客房管理系统2">
      酒店前台客房管理系统2 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#类的设计"><span class="nav-number">1.</span> <span class="nav-text">类的设计</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#客房编号的处理"><span class="nav-number">2.</span> <span class="nav-text">客房编号的处理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#客房编号"><span class="nav-number">2.1.</span> <span class="nav-text">客房编号</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#客房编号参数的解析"><span class="nav-number">2.2.</span> <span class="nav-text">客房编号参数的解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#客房编号的合法性判断"><span class="nav-number">2.3.</span> <span class="nav-text">客房编号的合法性判断</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="文予"
      src="/uploads/wenyu.png">
  <p class="site-author-name" itemprop="name">文予</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文予</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
